package com.dragon.smtp.service.impl;

import com.dragon.smtp.dao.UserDao;
import com.dragon.smtp.domain.Mail;
import com.dragon.smtp.dao.MailDao;
import com.dragon.smtp.domain.User;
import com.dragon.smtp.service.IMailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.smtp.util.DragonResult;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小龙
 * @since 2022-11-08
 */
@Service
public class MailServiceImpl extends ServiceImpl<MailDao, Mail> implements IMailService {
    @Autowired
    MailDao mailDao;

    @Autowired
    private UserDao userDao;

    @Override
    public DragonResult sendMail(Mail mail) {
        User user = userDao.selectByName(mail.getFromName());

        System.out.println(user);

        mail.setUserId(user.getId());
        System.out.println(user.getId());

        System.out.println(mail);
        boolean save = save(mail);
        if(save){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("发送失败");
        }
    }

    @Override
    public DragonResult getMail(User user) {

        List<Mail> mailByToName = mailDao.getMailByToName(user.getUsername());

        System.out.println(mailByToName);
        return DragonResult.ok(mailByToName!=null&&!mailByToName.isEmpty(),mailByToName);
    }

    //已发送邮件
    @Override
    public DragonResult getSentMail(User user) {
        List<Mail> SentMail = mailDao.getMailByFromName(user.getUsername());
        System.out.println(SentMail);
        return DragonResult.ok(SentMail!=null&&!SentMail.isEmpty(),SentMail);

    }


    @Override
    public DragonResult getDeletedMail(User user) {
        List<Mail> DeletedMail = mailDao.getMailByToName(user.getUsername());
        System.out.println(DeletedMail);
        return DragonResult.ok(DeletedMail!=null&&!DeletedMail.isEmpty(),DeletedMail);

    }


    @Override
    public DragonResult starMail(String id) {
        int i = mailDao.startMail(id);
        if(i>0){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("标记失败");
        }
    }

    @Override
    public DragonResult deleteMail(String id) {
        boolean b = removeById(id);
        if(b){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("删除失败");
        }
    }

    @Override
    public DragonResult markDeletedMail(String id) {
        int f = mailDao.markDeleteMail(id);
        if(f>0){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("删除失败");
        }
    }

    @Override
    public DragonResult restoreDeletedMail(String id) {
        int f = mailDao.restoreDeleteMail(id);
        if(f>0){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("恢复失败");
        }
    }

    @Override
    public DragonResult unstarMail(String id) {
        int i = mailDao.unstartMail(id);
        if(i>0){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("标记失败");
        }
    }

    @Override
    public DragonResult getStarMail(User user) {
        List<Mail> mailByToName = mailDao.getStarMailByToName(user.getUsername());

        System.out.println(mailByToName);
        return DragonResult.ok(mailByToName!=null&&!mailByToName.isEmpty(),mailByToName);
    }

    @Override
    public DragonResult sendDraftMail(Mail mail) {
        User user = userDao.selectByName(mail.getFromName());

        System.out.println(user);

        mail.setUserId(user.getId());
        mail.setDraft(1);
        System.out.println(user.getId());

        System.out.println(mail);
        boolean save = save(mail);
        if(save){
            return DragonResult.ok();
        } else {
            return DragonResult.fail("保存草稿失败");
        }
    }

    @Override
    public DragonResult getSentDraftMail(User user) {
        List<Mail> mailByToName = mailDao.getDraftMailByToName(user.getUsername());

        System.out.println(mailByToName);
        return DragonResult.ok(mailByToName!=null&&!mailByToName.isEmpty(),mailByToName);
    }
}
